﻿using Newtonsoft.Json;
using System;

namespace OperationPlugins.Json
{
    public static class JsonUtil
    {
        /// <summary>
        /// Serializes the specified object to a JSON string.
        /// </summary>
        /// <param name="value">The object to serialize.</param>
        /// <returns>A JSON string.</returns>
        public static string Serialize(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            string json = JsonConvert.SerializeObject(value);
            return json;
        }

        /// <summary>
        /// Deserializes the JSON to the specified .NET type.
        /// </summary>
        /// <param name="json">The JSON to deserialize.</param>
        /// <param name="type">The .NET type.</param>
        /// <returns>An object.</returns>
        public static object Deserialize(string json, Type type)
        {
            if (json == null)
            {
                throw new ArgumentNullException("json");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            object obj = JsonConvert.DeserializeObject(json, type);
            return obj;
        }

    }
}
